package com.cljz.makepassenger.present.activity;

import com.cljz.makepassenger.base.BaseP;
import com.cljz.makepassenger.model.net.api.Api;
import com.cljz.makepassenger.model.net.bean.ActivityIndexBean;
import com.cljz.makepassenger.model.net.bean.AdListBean;
import com.cljz.makepassenger.model.net.bean.GetUserInfoBean;
import com.cljz.makepassenger.model.net.bean.InviteListBean;
import com.cljz.makepassenger.model.net.bean.NoticeListBean;
import com.cljz.makepassenger.model.net.bean.ResponseBean;
import com.cljz.makepassenger.ui.activity.MainActivity;
import com.clzj.makepassenger.bean.UserInfo;

import java.util.HashMap;

import cn.droidlover.xdroidmvp.net.ApiSubcriber;
import cn.droidlover.xdroidmvp.net.NetError;
import cn.droidlover.xdroidmvp.net.XApi;
import cn.droidlover.xdroidmvp.net.diy.Base64Utils;
import okhttp3.RequestBody;

/**
 * Created by apple on 2017/4/5.
 */

public class MainP extends BaseP<MainActivity> {
    //获取最新的活动
    private boolean isFirst = true;

    public void getBanner() {
        HashMap<String, String> paramsMap = new HashMap<>();
        paramsMap.put("self_config", "banner_v22");
        String body = Base64Utils.enCode(gson.toJson(paramsMap));
        RequestBody requestBody = RequestBody.create(okhttp3.MediaType.parse("text/plain;charset=UTF-8"), body);
        Api.getResponseInfoAPI().getAdListData(requestBody)
                .compose(XApi.<AdListBean>getApiTransformer())         //异常处理
                .compose(XApi.<AdListBean>getScheduler())              //线程切换
                .compose(getV().<AdListBean>bindToLifecycle())
                .subscribe(new ApiSubcriber<AdListBean>() {
                    @Override
                    protected void onFail(NetError error) {
                    }

                    @Override
                    public void onNext(AdListBean adListBean) {
                        if ("0".equals(adListBean.getError_code())) {
                            if (adListBean.getData().size() != 0) {
                                getV().showBanner(adListBean.getData());
                            } else {
                                getV().showBannerErr(adListBean.getMsg());
                            }
                        } else if ("1000".equals(adListBean.getError_code())) {
                            getV().loginOvertime(1);
                        } else {
                            getV().showBannerErr(adListBean.getMsg());
                        }
                    }
                });
    }

//    public void getLetter() {
//        HashMap<String, String> paramsMap = new HashMap<>();
//        paramsMap.put("token", getToken());
//        paramsMap.put("city_name", "成都");
//        String body = Base64Utils.enCode(gson.toJson(paramsMap));
//        RequestBody requestBody = RequestBody.create(okhttp3.MediaType.parse("text/plain;charset=UTF-8"), body);
//        Api.getResponseInfoAPI().getIndexInfoData(requestBody)
//                .compose(XApi.<IndexInfoBean>getApiTransformer())
//                .compose(XApi.<IndexInfoBean>getScheduler())
//                .compose(getV().<IndexInfoBean>bindToLifecycle())
//                .subscribe(new ApiSubcriber<IndexInfoBean>() {
//                    @Override
//                    protected void onFail(NetError error) {
//                    }
//
//                    @Override
//                    public void onNext(IndexInfoBean indexInfoBean) {
//                        if ("0".equals(indexInfoBean.getError_code())) {
//                            getV().showLetter(indexInfoBean.getData());
//                        } else if ("1000".equals(indexInfoBean.getError_code())) {
//                            getV().loginOvertime(1);
//                        } else {
//                            getV().showLetterFail(indexInfoBean.getMsg());
//                        }
//                    }
//                });
//    }

    public void getInviteList(int page) {
        HashMap<String, String> paramsMap = new HashMap<>();
        paramsMap.put("token", getToken());
        paramsMap.put("page", String.valueOf(page));
        paramsMap.put("type", "index");
        String body = Base64Utils.enCode(gson.toJson(paramsMap));
        RequestBody requestBody = RequestBody.create(okhttp3.MediaType.parse("text/plain;charset=UTF-8"), body);
        Api.getResponseInfoAPI().getInviteListData(requestBody)
                .compose(XApi.<InviteListBean>getApiTransformer())
                .compose(XApi.<InviteListBean>getScheduler())
                .compose(getV().<InviteListBean>bindToLifecycle())
                .subscribe(new ApiSubcriber<InviteListBean>() {
                    @Override
                    protected void onFail(NetError error) {
                        getV().getInviterListFail(error.toString());
                    }

                    @Override
                    public void onNext(InviteListBean inviteListBean) {
                        if ("0".equals(inviteListBean.getError_code())) {
                            getV().getInviterListSucceed(inviteListBean.getData());
                            if (isFirst) {
                                getV().showLetter(inviteListBean.getData2());
                                isFirst = false;
                            }
                        } else if ("1000".equals(inviteListBean.getError_code())) {
                            getV().getXrvInvite().refreshComplete();
                            getV().loginOvertime(1);
                        } else {
                            getV().getInviterListFail(inviteListBean.getMsg());
                        }
                    }
                });


    }

    public void getMsgDel(String type) {
        HashMap<String, String> paramsMap = new HashMap<>();
        paramsMap.put("token", getToken());
        paramsMap.put("msg_type", type);
        String body = Base64Utils.enCode(gson.toJson(paramsMap));
        final RequestBody requestBody = RequestBody.create(okhttp3.MediaType.parse("text/plain;charset=UTF-8"), body);
        Api.getResponseInfoAPI().getMsgDelData(requestBody)
                .compose(XApi.<ResponseBean>getApiTransformer())
                .compose(XApi.<ResponseBean>getScheduler())
                .compose(getV().<ResponseBean>bindToLifecycle())
                .subscribe(new ApiSubcriber<ResponseBean>() {
                    @Override
                    protected void onFail(NetError error) {
                    }

                    @Override
                    public void onNext(ResponseBean responseBean) {
                        if ("0".equals(responseBean.getError_code())) {
                            getV().msgDelSucceed();
                        } else if ("1000".equals(responseBean.getError_code())) {
                            getV().loginOvertime(1);
                        } else {
                            getV().msgDelFail(responseBean.getMsg());
                        }
                    }
                });
    }

//    public void getWarnCancel(String join_id) {
//        HashMap<String, String> paramsMap = new HashMap<>();
//        paramsMap.put("token", getToken());
//        paramsMap.put("join_id", join_id);
//        String body = Base64Utils.enCode(gson.toJson(paramsMap));
//        final RequestBody requestBody = RequestBody.create(okhttp3.MediaType.parse("text/plain;charset=UTF-8"), body);
//        Api.getResponseInfoAPI().getWarnCancelData(requestBody)
//                .compose(XApi.<ResponseBean>getApiTransformer())
//                .compose(XApi.<ResponseBean>getScheduler())
//                .compose(getV().<ResponseBean>bindToLifecycle())
//                .subscribe(new ApiSubcriber<ResponseBean>() {
//                    @Override
//                    protected void onFail(NetError error) {
//                    }
//
//                    @Override
//                    public void onNext(ResponseBean responseBean) {
//                        if ("0".equals(responseBean.getError_code())) {
//                            getV().warnCancelSucceed();
//                        } else if ("1000".equals(responseBean.getError_code())) {
//                            getV().loginOvertime(1);
//                        } else {
//                            getV().warnCancelFail(responseBean.getMsg());
//                        }
//                    }
//                });
//    }

    public void getNoticeList(int page) {
        HashMap<String, String> paramsMap = new HashMap<>();
        paramsMap.put("token", getToken());
        paramsMap.put("page", String.valueOf(page));
        String body = Base64Utils.enCode(gson.toJson(paramsMap));
        RequestBody requestBody = RequestBody.create(okhttp3.MediaType.parse("text/plain;charset=UTF-8"), body);
        Api.getResponseInfoAPI().noticeListData(requestBody)
                .compose(XApi.<NoticeListBean>getApiTransformer())
                .compose(XApi.<NoticeListBean>getScheduler())
                .compose(getV().<NoticeListBean>bindToLifecycle())
                .subscribe(new ApiSubcriber<NoticeListBean>() {
                    @Override
                    protected void onFail(NetError error) {
                    }

                    @Override
                    public void onNext(NoticeListBean noticeListBean) {
                        if ("0".equals(noticeListBean.getError_code())) {
                            getV().getNoticeListSucceed(noticeListBean.getData());
                        } else if ("1000".equals(noticeListBean.getError_code())) {
                            getV().loginOvertime(1);
                        } else {
                            getV().getNoticeListFail(noticeListBean.getMsg());
                        }
                    }
                });
    }

    public void getActivityIndex() {
        HashMap<String, String> paramsMap = new HashMap<>();
        paramsMap.put("token", getToken());
        String body = Base64Utils.enCode(gson.toJson(paramsMap));
        RequestBody requestBody = RequestBody.create(okhttp3.MediaType.parse("text/plain;charset=UTF-8"), body);
        Api.getResponseInfoAPI().activityIndexData(requestBody)
                .compose(XApi.<ActivityIndexBean>getApiTransformer())
                .compose(XApi.<ActivityIndexBean>getScheduler())
                .compose(getV().<ActivityIndexBean>bindToLifecycle())
                .subscribe(new ApiSubcriber<ActivityIndexBean>() {
                    @Override
                    protected void onFail(NetError error) {
                    }

                    @Override
                    public void onNext(ActivityIndexBean activityIndexBean) {
                        if ("0".equals(activityIndexBean.getError_code())) {
                            getV().getActivityIndexSucceed(activityIndexBean.getData());
                        } else if ("1000".equals(activityIndexBean.getError_code())) {
                            getV().loginOvertime(1);
                        } else {
                            getV().getActivityIndexFail(activityIndexBean.getMsg());
                        }
                    }
                });
    }


    public void getUserInfo() {
        HashMap<String, String> paramsMap = new HashMap<>();
        paramsMap.put("token", getToken());
        String body = Base64Utils.enCode(gson.toJson(paramsMap));
        RequestBody requestBody = RequestBody.create(okhttp3.MediaType.parse("text/plain;charset=UTF-8"), body);
        Api.getResponseInfoAPI().getUserInfoData(requestBody)
                .compose(XApi.<GetUserInfoBean>getApiTransformer())
                .compose(XApi.<GetUserInfoBean>getScheduler())
                .compose(getV().<GetUserInfoBean>bindToLifecycle())
                .subscribe(new ApiSubcriber<GetUserInfoBean>() {
                    @Override
                    protected void onFail(NetError error) {
                    }

                    @Override
                    public void onNext(GetUserInfoBean getUserInfoBean) {
                        if ("0".equals(getUserInfoBean.getError_code())) {
                            GetUserInfoBean.DataBean dataBean = getUserInfoBean.getData().get(0);
                            UserInfo userInfo = getLocalUserInfo(getToken());
                            userInfo.setBirthday(dataBean.getBirthday());
                            userInfo.setUser_logo(dataBean.getUser_logo());
                            userInfo.setUser_nickname(dataBean.getUser_nickname());
                            userInfo.setUser_phone(dataBean.getUser_phone().substring(0, 3) + "****" + dataBean.getUser_phone().substring(7));
                            userInfo.setUser_sex(dataBean.getUser_sex());
                            userInfo.setEid(dataBean.getEid());
                            userInfo.setAge(dataBean.getAge());
                            userInfo.setPayment_password(dataBean.getPayment_password());
                            userInfo.setAlipay(dataBean.getAlipay());
                            userInfo.setUser_name(dataBean.getUser_name());
                            userInfo.setUser_points(dataBean.getUser_points());
                            userInfo.setRatio(dataBean.getRatio());
                            updateDB(userInfo);
                        } else if ("1000".equals(getUserInfoBean.getError_code())) {
                            getV().loginOvertime(1);
                        } else {
                            getV().getUserInfoFail(getUserInfoBean.getMsg());
                        }
                    }
                });
    }

}
